{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Step-62"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%config InlineBackend.figure_formats = ['svg']\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import h5py\n",
    "import matplotlib.pyplot as plt\n",
    "import subprocess\n",
    "import scipy.constants as constants\n",
    "import scipy.optimize\n",
    "\n",
    "# This considerably reduces the size of the svg data\n",
    "plt.rcParams['svg.fonttype'] = 'none'\n",
    "\n",
    "h5_file = h5py.File('results.h5', 'w')\n",
    "data = h5_file.create_group('data')\n",
    "displacement = data.create_group('displacement')\n",
    "calibration = data.create_group('calibration')\n",
    "\n",
    "# Set the parameters\n",
    "for group in [displacement, calibration]:\n",
    "    # Dimensions of the domain\n",
    "    # The waveguide length is equal to dimension_x\n",
    "    group.attrs['dimension_x'] = 2e-5\n",
    "    # The waveguide width is equal to dimension_y\n",
    "    group.attrs['dimension_y'] = 2e-8\n",
    "    \n",
    "    # Position of the probe that we use to measure the flux\n",
    "    group.attrs['probe_pos_x']   = 8e-6\n",
    "    group.attrs['probe_pos_y']   = 0\n",
    "    group.attrs['probe_width_y'] = 2e-08\n",
    "    \n",
    "    # Number of points in the probe\n",
    "    group.attrs['nb_probe_points'] = 5\n",
    "    \n",
    "    # Global refinement\n",
    "    group.attrs['grid_level'] = 1\n",
    "\n",
    "    # Cavity\n",
    "    group.attrs['cavity_resonance_frequency'] = 20e9\n",
    "    group.attrs['nb_mirror_pairs']            = 15\n",
    "\n",
    "    # Material\n",
    "    group.attrs['poissons_ratio'] = 0.27\n",
    "    group.attrs['youngs_modulus'] = 270000000000.0\n",
    "    group.attrs['material_a_rho'] = 3200\n",
    "    if group == displacement:\n",
    "        group.attrs['material_b_rho'] = 2000\n",
    "    else:\n",
    "        group.attrs['material_b_rho'] = 3200   \n",
    "    group.attrs['lambda'] = (group.attrs['youngs_modulus'] * group.attrs['poissons_ratio'] /\n",
    "                           ((1 + group.attrs['poissons_ratio']) *\n",
    "                           (1 - 2 * group.attrs['poissons_ratio'])))\n",
    "    group.attrs['mu']= (group.attrs['youngs_modulus'] / (2 * (1 + group.attrs['poissons_ratio'])))\n",
    "\n",
    "    # Force\n",
    "    group.attrs['max_force_amplitude'] = 1e26\n",
    "    group.attrs['force_sigma_x']       = 1e-7\n",
    "    group.attrs['force_sigma_y']       = 1\n",
    "    group.attrs['max_force_width_x']   = 3e-7\n",
    "    group.attrs['max_force_width_y']   = 2e-8\n",
    "    group.attrs['force_x_pos']         = -8e-6\n",
    "    group.attrs['force_y_pos']         = 0\n",
    "\n",
    "    # PML\n",
    "    group.attrs['pml_x']            = True\n",
    "    group.attrs['pml_y']            = False\n",
    "    group.attrs['pml_width_x']      = 1.8e-6\n",
    "    group.attrs['pml_width_y']      = 5e-7\n",
    "    group.attrs['pml_coeff']        = 1.6\n",
    "    group.attrs['pml_coeff_degree'] = 2\n",
    "\n",
    "    # Frequency sweep\n",
    "    group.attrs['center_frequency']    = 20e9\n",
    "    group.attrs['frequency_range']     = 0.5e9\n",
    "    group.attrs['start_frequency']     = group.attrs['center_frequency'] - group.attrs['frequency_range'] / 2\n",
    "    group.attrs['stop_frequency']      = group.attrs['center_frequency'] + group.attrs['frequency_range'] / 2\n",
    "    group.attrs['nb_frequency_points'] = 400\n",
    "\n",
    "    # Other parameters\n",
    "    if group == displacement:\n",
    "        group.attrs['simulation_name'] = 'phononic_cavity_displacement'\n",
    "    else:\n",
    "        group.attrs['simulation_name'] = 'phononic_cavity_calibration'\n",
    "    group.attrs['save_vtu_files'] = False\n",
    "    \n",
    "h5_file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run simulation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CompletedProcess(args=['mpiexec', './step-62'], returncode=0)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Now you can run your simulation locally or in a cluster\n",
    "subprocess.run(['mpiexec','./step-62'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Analyze data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Created with matplotlib (http://matplotlib.org/) -->\n",
       "<svg height=\"290pt\" version=\"1.1\" viewBox=\"0 0 389 290\" width=\"389pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       " <defs>\n",
       "  <style type=\"text/css\">\n",
       "*{stroke-linecap:butt;stroke-linejoin:round;}\n",
       "  </style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 290.75175 \n",
       "L 389.28125 290.75175 \n",
       "L 389.28125 0 \n",
       "L 0 0 \n",
       "z\n",
       "\" style=\"fill:none;\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 43.78125 253.1955 \n",
       "L 378.58125 253.1955 \n",
       "L 378.58125 35.7555 \n",
       "L 43.78125 35.7555 \n",
       "z\n",
       "\" style=\"fill:#ffffff;\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L 0 3.5 \n",
       "\" id=\"mb3d45af095\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"89.435795\" xlink:href=\"#mb3d45af095\" y=\"253.1955\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_1\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:middle;\" transform=\"rotate(-0, 89.435795, 267.793937)\" x=\"89.435795\" y=\"267.793937\">19.8</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_2\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"150.308523\" xlink:href=\"#mb3d45af095\" y=\"253.1955\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_2\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:middle;\" transform=\"rotate(-0, 150.308523, 267.793937)\" x=\"150.308523\" y=\"267.793937\">19.9</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_3\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"211.18125\" xlink:href=\"#mb3d45af095\" y=\"253.1955\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_3\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:middle;\" transform=\"rotate(-0, 211.18125, 267.793937)\" x=\"211.18125\" y=\"267.793937\">20.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_4\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"272.053977\" xlink:href=\"#mb3d45af095\" y=\"253.1955\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_4\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:middle;\" transform=\"rotate(-0, 272.053977, 267.793937)\" x=\"272.053977\" y=\"267.793937\">20.1</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_5\">\n",
       "     <g id=\"line2d_5\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"332.926705\" xlink:href=\"#mb3d45af095\" y=\"253.1955\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_5\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:middle;\" transform=\"rotate(-0, 332.926705, 267.793937)\" x=\"332.926705\" y=\"267.793937\">20.2</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_6\">\n",
       "     <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:middle;\" transform=\"rotate(-0, 211.18125, 281.472062)\" x=\"211.18125\" y=\"281.472062\">frequency (GHz)</text>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_6\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L -3.5 0 \n",
       "\" id=\"m626a42a4d9\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"43.78125\" xlink:href=\"#m626a42a4d9\" y=\"243.323682\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_7\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:end;\" transform=\"rotate(-0, 36.78125, 247.1229)\" x=\"36.78125\" y=\"247.1229\">0.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"43.78125\" xlink:href=\"#m626a42a4d9\" y=\"203.726948\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_8\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:end;\" transform=\"rotate(-0, 36.78125, 207.526167)\" x=\"36.78125\" y=\"207.526167\">0.2</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_8\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"43.78125\" xlink:href=\"#m626a42a4d9\" y=\"164.130215\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_9\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:end;\" transform=\"rotate(-0, 36.78125, 167.929434)\" x=\"36.78125\" y=\"167.929434\">0.4</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_9\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"43.78125\" xlink:href=\"#m626a42a4d9\" y=\"124.533482\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_10\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:end;\" transform=\"rotate(-0, 36.78125, 128.332701)\" x=\"36.78125\" y=\"128.332701\">0.6</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_5\">\n",
       "     <g id=\"line2d_10\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"43.78125\" xlink:href=\"#m626a42a4d9\" y=\"84.936749\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_11\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:end;\" transform=\"rotate(-0, 36.78125, 88.735968)\" x=\"36.78125\" y=\"88.735968\">0.8</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_6\">\n",
       "     <g id=\"line2d_11\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"43.78125\" xlink:href=\"#m626a42a4d9\" y=\"45.340016\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_12\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:end;\" transform=\"rotate(-0, 36.78125, 49.139235)\" x=\"36.78125\" y=\"49.139235\">1.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_13\">\n",
       "     <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:middle;\" transform=\"rotate(-90, 14.798438, 144.4755)\" x=\"14.798438\" y=\"144.4755\">amplitude^2 (a.u.)</text>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_12\">\n",
       "    <path clip-path=\"url(#p020971ab79)\" d=\"M 58.999432 243.309983 \n",
       "L 170.370587 243.184651 \n",
       "L 187.152542 242.980594 \n",
       "L 194.780703 242.69493 \n",
       "L 199.3576 242.309509 \n",
       "L 202.408864 241.815212 \n",
       "L 204.697313 241.162397 \n",
       "L 206.222945 240.460693 \n",
       "L 206.985761 239.974925 \n",
       "L 207.748577 239.355064 \n",
       "L 208.511394 238.547294 \n",
       "L 209.27421 237.468163 \n",
       "L 210.037026 235.982944 \n",
       "L 210.799842 233.863454 \n",
       "L 211.562658 230.700476 \n",
       "L 212.325474 225.708001 \n",
       "L 213.08829 217.249328 \n",
       "L 213.851106 201.609491 \n",
       "L 214.613923 169.958876 \n",
       "L 216.139555 45.639136 \n",
       "L 216.902371 96.886661 \n",
       "L 217.665187 164.187692 \n",
       "L 218.428003 198.841602 \n",
       "L 219.190819 215.822253 \n",
       "L 219.953636 224.900029 \n",
       "L 220.716452 230.2053 \n",
       "L 221.479268 233.540209 \n",
       "L 222.242084 235.761076 \n",
       "L 223.0049 237.309603 \n",
       "L 223.767716 238.430172 \n",
       "L 224.530532 239.266147 \n",
       "L 226.056165 240.40596 \n",
       "L 227.581797 241.126323 \n",
       "L 229.107429 241.609914 \n",
       "L 231.395877 242.083283 \n",
       "L 234.447142 242.460909 \n",
       "L 239.024039 242.770487 \n",
       "L 246.6522 243.011581 \n",
       "L 260.38289 243.174225 \n",
       "L 292.421168 243.272107 \n",
       "L 363.363068 243.308298 \n",
       "L 363.363068 243.308298 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-linecap:square;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"line2d_13\">\n",
       "    <path clip-path=\"url(#p020971ab79)\" d=\"M 58.999432 243.311864 \n",
       "L 170.370587 243.185936 \n",
       "L 187.152542 242.981507 \n",
       "L 194.780703 242.695484 \n",
       "L 199.3576 242.309692 \n",
       "L 202.408864 241.815013 \n",
       "L 204.697313 241.16178 \n",
       "L 206.222945 240.459696 \n",
       "L 206.985761 239.973693 \n",
       "L 207.748577 239.353559 \n",
       "L 208.511394 238.545467 \n",
       "L 209.27421 237.465955 \n",
       "L 210.037026 235.980277 \n",
       "L 210.799842 233.860233 \n",
       "L 211.562658 230.696589 \n",
       "L 212.325474 225.703333 \n",
       "L 213.08829 217.243853 \n",
       "L 213.851106 201.60361 \n",
       "L 214.613923 169.954664 \n",
       "L 216.139555 45.641554 \n",
       "L 216.902371 96.880455 \n",
       "L 217.665187 164.188236 \n",
       "L 218.428003 198.84626 \n",
       "L 219.190819 215.828198 \n",
       "L 219.953636 224.906185 \n",
       "L 220.716452 230.211307 \n",
       "L 221.479268 233.545957 \n",
       "L 222.242084 235.766548 \n",
       "L 223.0049 237.314812 \n",
       "L 223.767716 238.435143 \n",
       "L 224.530532 239.270905 \n",
       "L 226.056165 240.410358 \n",
       "L 227.581797 241.130436 \n",
       "L 229.107429 241.613796 \n",
       "L 231.395877 242.086894 \n",
       "L 234.447142 242.464255 \n",
       "L 239.024039 242.773559 \n",
       "L 246.6522 243.014372 \n",
       "L 260.38289 243.176752 \n",
       "L 292.421168 243.274404 \n",
       "L 363.363068 243.310534 \n",
       "L 363.363068 243.310534 \n",
       "\" style=\"fill:none;stroke:#ff7f0e;stroke-linecap:square;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 43.78125 253.1955 \n",
       "L 43.78125 35.7555 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 378.58125 253.1955 \n",
       "L 378.58125 35.7555 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_5\">\n",
       "    <path d=\"M 43.78125 253.1955 \n",
       "L 378.58125 253.1955 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_6\">\n",
       "    <path d=\"M 43.78125 35.7555 \n",
       "L 378.58125 35.7555 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"text_14\">\n",
       "    <text style=\"font-family:DejaVu Sans;font-size:12px;font-style:normal;font-weight:400;\" transform=\"translate(171.555 16.318125)\">Transmission</text>\n",
       "    <text style=\"font-family:DejaVu Sans;font-size:12px;font-style:normal;font-weight:400;\" transform=\"translate(109.03875 29.7555)\">freq = 20.00815GHz Q = 5046.91</text>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p020971ab79\">\n",
       "   <rect height=\"217.44\" width=\"334.8\" x=\"43.78125\" y=\"35.7555\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f785c3ecf28>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Created with matplotlib (http://matplotlib.org/) -->\n",
       "<svg height=\"290pt\" version=\"1.1\" viewBox=\"0 0 397 290\" width=\"397pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       " <defs>\n",
       "  <style type=\"text/css\">\n",
       "*{stroke-linecap:butt;stroke-linejoin:round;}\n",
       "  </style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 290.75175 \n",
       "L 397.660937 290.75175 \n",
       "L 397.660937 0 \n",
       "L 0 0 \n",
       "z\n",
       "\" style=\"fill:none;\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 52.160938 253.1955 \n",
       "L 386.960938 253.1955 \n",
       "L 386.960938 35.7555 \n",
       "L 52.160938 35.7555 \n",
       "z\n",
       "\" style=\"fill:#ffffff;\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L 0 3.5 \n",
       "\" id=\"m71cbb29084\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"97.815483\" xlink:href=\"#m71cbb29084\" y=\"253.1955\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_1\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:middle;\" transform=\"rotate(-0, 97.815483, 267.793937)\" x=\"97.815483\" y=\"267.793937\">19.8</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_2\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"158.68821\" xlink:href=\"#m71cbb29084\" y=\"253.1955\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_2\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:middle;\" transform=\"rotate(-0, 158.68821, 267.793937)\" x=\"158.68821\" y=\"267.793937\">19.9</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_3\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"219.560938\" xlink:href=\"#m71cbb29084\" y=\"253.1955\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_3\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:middle;\" transform=\"rotate(-0, 219.560938, 267.793937)\" x=\"219.560938\" y=\"267.793937\">20.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_4\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"280.433665\" xlink:href=\"#m71cbb29084\" y=\"253.1955\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_4\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:middle;\" transform=\"rotate(-0, 280.433665, 267.793937)\" x=\"280.433665\" y=\"267.793937\">20.1</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_5\">\n",
       "     <g id=\"line2d_5\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"341.306392\" xlink:href=\"#m71cbb29084\" y=\"253.1955\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_5\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:middle;\" transform=\"rotate(-0, 341.306392, 267.793937)\" x=\"341.306392\" y=\"267.793937\">20.2</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_6\">\n",
       "     <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:middle;\" transform=\"rotate(-0, 219.560938, 281.472062)\" x=\"219.560938\" y=\"281.472062\">frequency (GHz)</text>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_6\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L -3.5 0 \n",
       "\" id=\"m02a51f3213\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"52.160938\" xlink:href=\"#m02a51f3213\" y=\"222.90313\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_7\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:end;\" transform=\"rotate(-0, 45.160938, 226.702349)\" x=\"45.160938\" y=\"226.702349\">−1.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"52.160938\" xlink:href=\"#m02a51f3213\" y=\"186.940536\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_8\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:end;\" transform=\"rotate(-0, 45.160938, 190.739755)\" x=\"45.160938\" y=\"190.739755\">−0.5</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_8\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"52.160938\" xlink:href=\"#m02a51f3213\" y=\"150.977942\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_9\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:end;\" transform=\"rotate(-0, 45.160938, 154.777161)\" x=\"45.160938\" y=\"154.777161\">0.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_9\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"52.160938\" xlink:href=\"#m02a51f3213\" y=\"115.015348\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_10\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:end;\" transform=\"rotate(-0, 45.160938, 118.814566)\" x=\"45.160938\" y=\"118.814566\">0.5</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_5\">\n",
       "     <g id=\"line2d_10\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"52.160938\" xlink:href=\"#m02a51f3213\" y=\"79.052753\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_11\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:end;\" transform=\"rotate(-0, 45.160938, 82.851972)\" x=\"45.160938\" y=\"82.851972\">1.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_6\">\n",
       "     <g id=\"line2d_11\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"52.160938\" xlink:href=\"#m02a51f3213\" y=\"43.090159\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_12\">\n",
       "      <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:end;\" transform=\"rotate(-0, 45.160938, 46.889378)\" x=\"45.160938\" y=\"46.889378\">1.5</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_13\">\n",
       "     <text style=\"font-family:DejaVu Sans;font-size:10px;font-style:normal;font-weight:400;text-anchor:middle;\" transform=\"rotate(-90, 14.798438, 144.4755)\" x=\"14.798438\" y=\"144.4755\">phase (rad)</text>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_12\">\n",
       "    <path clip-path=\"url(#pbed3d3adf0)\" d=\"M 67.379119 166.088347 \n",
       "L 130.692858 202.273193 \n",
       "L 159.679871 218.610148 \n",
       "L 176.461826 227.843396 \n",
       "L 187.141252 233.502139 \n",
       "L 194.769413 237.318877 \n",
       "L 200.109126 239.771074 \n",
       "L 203.923207 241.32225 \n",
       "L 206.974471 242.35993 \n",
       "L 209.26292 242.949164 \n",
       "L 211.551368 243.282275 \n",
       "L 213.077 243.287383 \n",
       "L 214.602633 243.027369 \n",
       "L 215.365449 242.757524 \n",
       "L 216.128265 242.361532 \n",
       "L 216.891081 241.802844 \n",
       "L 217.653897 241.029444 \n",
       "L 218.416713 239.964816 \n",
       "L 219.179529 238.491884 \n",
       "L 219.942346 236.422753 \n",
       "L 220.705162 233.437761 \n",
       "L 221.467978 228.952754 \n",
       "L 222.230794 221.805147 \n",
       "L 222.99361 209.473433 \n",
       "L 223.756426 186.519787 \n",
       "L 225.282058 106.374166 \n",
       "L 226.044875 81.576783 \n",
       "L 226.807691 68.308942 \n",
       "L 227.570507 60.700773 \n",
       "L 228.333323 55.968132 \n",
       "L 229.096139 52.837706 \n",
       "L 229.858955 50.676796 \n",
       "L 230.621771 49.142553 \n",
       "L 231.384588 48.035069 \n",
       "L 232.147404 47.230592 \n",
       "L 232.91022 46.64868 \n",
       "L 233.673036 46.23489 \n",
       "L 235.198668 45.769903 \n",
       "L 236.7243 45.639136 \n",
       "L 238.249933 45.732212 \n",
       "L 240.538381 46.152534 \n",
       "L 242.82683 46.797943 \n",
       "L 245.878094 47.883969 \n",
       "L 249.692175 49.472364 \n",
       "L 255.031888 51.954866 \n",
       "L 261.897233 55.400177 \n",
       "L 271.813842 60.644621 \n",
       "L 286.307349 68.59029 \n",
       "L 308.429017 81.002467 \n",
       "L 345.044191 101.836603 \n",
       "L 371.742756 117.117192 \n",
       "L 371.742756 117.117192 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-linecap:square;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 52.160938 253.1955 \n",
       "L 52.160938 35.7555 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 386.960938 253.1955 \n",
       "L 386.960938 35.7555 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_5\">\n",
       "    <path d=\"M 52.160938 253.1955 \n",
       "L 386.960938 253.1955 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_6\">\n",
       "    <path d=\"M 52.160938 35.7555 \n",
       "L 386.960938 35.7555 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"text_14\">\n",
       "    <text style=\"font-family:DejaVu Sans;font-size:12px;font-style:normal;font-weight:400;\" transform=\"translate(123.457813 16.318125)\">Phase (transmission coefficient)</text>\n",
       "    <text style=\"font-family:DejaVu Sans;font-size:12px;font-style:normal;font-weight:400;\" transform=\"translate(219.560938 29.7555)\"/>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"pbed3d3adf0\">\n",
       "   <rect height=\"217.44\" width=\"334.8\" x=\"52.160938\" y=\"35.7555\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f785eabc5c0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "h5_file = h5py.File('results.h5', 'r')\n",
    "data = h5_file['data']\n",
    "\n",
    "# Gaussian function that we use to fit the resonance\n",
    "def resonance_f(freq, freq_m, quality_factor, max_amplitude):\n",
    "    omega = 2 * constants.pi * freq\n",
    "    omega_m = 2 * constants.pi * freq_m\n",
    "    gamma = omega_m / quality_factor\n",
    "    return max_amplitude * omega_m**2 * gamma**2 / (((omega_m**2 - omega**2)**2 + gamma**2 * omega**2))\n",
    "\n",
    "frequency = data['displacement']['frequency'][...]\n",
    "# Average the probe points\n",
    "displacement = np.mean(data['displacement']['displacement'], axis=0)\n",
    "calibration_displacement = np.mean(data['calibration']['displacement'], axis=0)\n",
    "reflection_coefficient = displacement / calibration_displacement\n",
    "reflectivity = (np.abs(np.mean(data['displacement']['displacement'][...]**2, axis=0))/\n",
    "                np.abs(np.mean(data['calibration']['displacement'][...]**2, axis=0)))\n",
    "\n",
    "try:\n",
    "    x_data = frequency\n",
    "    y_data = reflectivity\n",
    "    quality_factor_guess = 1e3\n",
    "    freq_guess = x_data[np.argmax(y_data)]\n",
    "    amplitude_guess = np.max(y_data)\n",
    "    fit_result, covariance = scipy.optimize.curve_fit(resonance_f, x_data, y_data,\n",
    "                                                      [freq_guess, quality_factor_guess, amplitude_guess])\n",
    "    freq_m = fit_result[0]\n",
    "    quality_factor = np.abs(fit_result[1])\n",
    "    max_amplitude = fit_result[2]\n",
    "    y_data_fit = resonance_f(x_data, freq_m, quality_factor, max_amplitude)\n",
    "\n",
    "    fig = plt.figure()\n",
    "    plt.plot(frequency / 1e9, reflectivity, frequency / 1e9, y_data_fit)\n",
    "    plt.xlabel('frequency (GHz)')\n",
    "    plt.ylabel('amplitude^2 (a.u.)')\n",
    "    plt.title('Transmission\\n' + 'freq = ' + \"%.7g\" % (freq_guess / 1e9) + 'GHz Q = ' + \"%.6g\" % quality_factor)\n",
    "except:\n",
    "    fig = plt.figure()\n",
    "    plt.plot(frequency / 1e9, reflectivity)\n",
    "    plt.xlabel('frequency (GHz)')\n",
    "    plt.ylabel('amplitude^2 (a.u.)')\n",
    "    plt.title('Transmission')\n",
    "\n",
    "fig = plt.figure()\n",
    "plt.plot(frequency / 1e9, np.angle(reflection_coefficient))\n",
    "plt.xlabel('frequency (GHz)')\n",
    "plt.ylabel('phase (rad)')\n",
    "plt.title('Phase (transmission coefficient)\\n')\n",
    "\n",
    "plt.show()\n",
    "h5_file.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
